import { defineConfig } from "vite";
import reactRefresh from "@vitejs/plugin-react-refresh";
import styleImport from "vite-plugin-style-import";
import { resolve } from "path";

export default () => {
    return defineConfig({
        // base: loadEnv(mode, process.cwd()).REACT_APP_API_URL,
        plugins: [
            reactRefresh(),
            // 样式按需引入
            styleImport({
                libs: [
                    {
                        libraryName: "antd",
                        esModule: true,
                        resolveStyle: (name) => {
                            return `antd/es/${name}/style/index`;
                        },
                    },
                    {
                        libraryName: "zarm",
                        esModule: true,
                        resolveStyle: (name) => {
                            return `zarm/es/${name}/style/css`;
                        },
                    },
                ],
            }),
        ],
        // 配置之后无需写入import React from 'react'
        /* esbuild: {
        jsxInject: `import React from 'react'`,
    }, */
        resolve: {
            // 如果报错__dirname找不到，需要安装node,执行yarn add @types/node --save-dev
            alias: [
                { find: "@", replacement: resolve(__dirname, "src") },
                { find: "screens", replacement: resolve(__dirname, "src/screens") },
                { find: "components", replacement: resolve(__dirname, "src/components") },
                { find: "assets", replacement: resolve(__dirname, "src/assets") },
                { find: "apis", replacement: resolve(__dirname, "src/apis") },
                { find: "config", replacement: resolve(__dirname, "src/config") },
            ],
        },
        server: {
            port: 3001,
        },
        css: {
            preprocessorOptions: {
                less: {
                    // 支持内联JS
                    javascriptEnabled: true,
                    // 重写less变量，定制样式
                    modifyVars: {
                        "@primary-color": "#1890ff",
                    },
                },
            },
        },
        // 解决process全局配置变量无法读取问题
        /* define: {
        "process.env": {},
    }, */
    });
};
